﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace ERSWindowsFormsApp
{
    public partial class EmployeeRecordForm : Form
    {
        private TreeNode tvRootNode;

        public EmployeeRecordForm()
        {
            InitializeComponent();
        }

        private void EmployeeRecordForm_Load(object sender, EventArgs e)
        {
            PopulateTreeView();
            IntializeListControl();
        }

        private void PopulateTreeView()
        {
            statusBarPanel1.Text = "Refreshing employee code ..Please wait.. ";
            this.Cursor = Cursors.WaitCursor;
            treeView1.Nodes.Clear();
            tvRootNode = new TreeNode("Employee Record");
            this.Cursor = Cursors.Default;
            treeView1.Nodes.Add(tvRootNode);
            TreeNodeCollection nodeCollection = tvRootNode.Nodes;
            String strVal = "";
            XmlTextReader reader = new XmlTextReader("D:\\MyTutorials\\CASE\\2024-25.2\\mywork\\CMSWindowsFormsApp2\\ERSWindowsFormsApp\\EmpRec.xml");
            reader.MoveToElement();//read first element in xml
            try {

                while (reader.Read()) {
                    if (reader.HasAttributes && reader.NodeType == XmlNodeType.Element) {
                        reader.MoveToElement();
                        reader.MoveToElement();
                        reader.MoveToAttribute("Id");

                        strVal = reader.Value;
                        reader.Read();
                        reader.Read();
                        if (reader.Name == "Dept")
                        {
                            reader.Read();

                        }
                        TreeNode treeNode = new TreeNode(strVal);
                        nodeCollection.Add(treeNode);



                    }
                }
                statusBarPanel1.Text = "Click on an emplyoee code to see their record.";

            }
            catch (Exception ex) {
                MessageBox.Show(" ERROR: " + ex.Message);
            }
        }
        protected void IntializeListControl() {
            listView1.Clear();
            listView1.Columns.Add("Employee Name",100,HorizontalAlignment.Left);
            listView1.Columns.Add("Date of Join", 120, HorizontalAlignment.Left);

            listView1.Columns.Add("Gread", 105, HorizontalAlignment.Left);

            listView1.Columns.Add("Salary", 100, HorizontalAlignment.Left);


        }
        protected void PopoulateListView(TreeNode currNode) {
            IntializeListControl();
            //read- xml 
            XmlTextReader listRead = new XmlTextReader("D:\\MyTutorials\\CASE\\2024-25.2\\mywork\\CMSWindowsFormsApp2\\ERSWindowsFormsApp\\EmpRec.xml");//path
                                                                                                                                                       //<EmpRecordsData>
            listRead.MoveToElement();//<EmpRecordsData>
            while (listRead.Read()) {
                string strNodeName;
                string strNodePath;
                string name;
                string doj;
                string grade;
                string salary;
                string[] strItemArray = new string[4];

                listRead.MoveToFirstAttribute();//Id="E001"
                strNodeName = listRead.Value;
                strNodePath = currNode.FullPath.Remove(0,17);//remove the first 17 charectes
                if (strNodePath == strNodeName) {
                    ListViewItem lvi;
                    listRead.MoveToNextAttribute();
                    name = listRead.Value;
                    lvi = listView1.Items.Add(name);
                    listRead.Read();//>
                    listRead.Read();//<
                  
                    listRead.MoveToNextAttribute();//DateofJoin="03-22-1999
                    doj = listRead.Value;
                    lvi.SubItems.Add(doj);

                    listRead.MoveToNextAttribute();//Gread="B"
                    grade = listRead.Value;
                    lvi.SubItems.Add(grade);

                    listRead.MoveToNextAttribute();//Salary="2500"
                    salary = listRead.Value;
                    lvi.SubItems.Add(salary);

                    listRead.MoveToNextAttribute();
                    listRead.MoveToElement();
                    listRead.ReadString();




                }






            }

        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode currNode = e.Node;
            if (tvRootNode==currNode) {

                IntializeListControl();
                statusBarPanel1.Text = "Double clieck the empyloee Recods";
                return;

            } else {
                statusBarPanel1.Text = "Clcick an employee code to view individual records";
            
            }
            PopoulateListView(currNode);

        }
    }
}
